

include build.mk

# 
# Toolchain
# ============
# 
AS      = $(CROSS_COMPILE)as$(CROSS_POSTFIX)
LD      = $(CROSS_COMPILE)ld$(CROSS_POSTFIX)
CC      = $(CROSS_COMPILE)gcc$(CROSS_POSTFIX)
CPP     = $(CC) -E
AR      = $(CROSS_COMPILE)ar$(CROSS_POSTFIX)
NM      = $(CROSS_COMPILE)nm$(CROSS_POSTFIX)
LDR     = $(CROSS_COMPILE)ldr$(CROSS_POSTFIX)
STRIP   = $(CROSS_COMPILE)strip$(CROSS_POSTFIX)
OBJCOPY = $(CROSS_COMPILE)objcopy$(CROSS_POSTFIX)
OBJDUMP = $(CROSS_COMPILE)objdump$(CROSS_POSTFIX)

export AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP

PYTHON  = python

ifeq ($(ARCH),)
  ARCH = x86
else
include arch/$(ARCH)/config.mk
endif

QESFLIB = libqesf-$(ARCH).a

all: preparse build

preparse: prepare_dir

prepare_dir: $(BUILD_DIR) $(BUILD_OJB_DIR)

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

$(BUILD_OJB_DIR):
	mkdir -p $(BUILD_OJB_DIR)

build: qesf_parts_build qesf_library_build qesf_library_install

# 
# Kconfig
# ============
#
KCONFIG_TOP         = Kconfig
KCONFIG_OUT_DOT     = .config
MK_AUTOCONFIG       = autoconfig.mk
ifneq ($(wildcard $(KCONFIG_OUT_DOT)),)
  KCONFIG_INPUT     = $(KCONFIG_OUT_DOT)
  KCONFIG_HANDWRITE = 
else
  KCONFIG_INPUT     = $(BOARD_DEFCONFIG)
  KCONFIG_HANDWRITE = --handwritten-input-configs
endif
ifneq ($(wildcard $(MK_AUTOCONFIG)),)
  $(warning "include $(MK_AUTOCONFIG)")
  include $(MK_AUTOCONFIG)
endif
KCONFIG_OUT_HEADER_DIR = $(PROJECT_ROOT)/include/generated
KCONFIG_OUT_HEADER  = $(KCONFIG_OUT_HEADER_DIR)/autoconfig.h
KCONFIG_OUT_LIST    = kconfig_source.list

$(KCONFIG_OUT_HEADER_DIR):
	mkdir -p include/generated

#
# target menuconfig
# --------------------
#   load kconfig top file to generate .config
#
PHONY += menuconfig
menuconfig:
	@echo "menuconfig"
	$(PYTHON) scripts/kconfig/menuconfig.py $(KCONFIG_TOP)
	$(MAKE) autoconfig
#
# target autoconfig
# --------------------
#   load .config to generate:
#       1. include/generated/autoconfig.h
#       2. autoconfig.mk
#
PHONY += autoconfig
autoconfig: $(KCONFIG_OUT_DOT) $(KCONFIG_OUT_HEADER_DIR)
	@echo "autoconfig"
	$(PYTHON) scripts/kconfig/kconfig.py $(KCONFIG_HANDWRITE) \
		$(KCONFIG_TOP) $(KCONFIG_OUT_DOT) $(KCONFIG_OUT_HEADER) \
		$(KCONFIG_OUT_LIST) $(KCONFIG_INPUT)
	$(PYTHON) scripts/kconfig/autoconfig.py .config $(MK_AUTOCONFIG)

qesf-parts-y = 
qesf-parts-y += common
qesf-parts-$(CONFIG_NET) += net
qesf-parts-$(CONFIG_DM) += device
qesf-parts-$(CONFIG_ISP) += isp
qesf-parts-y := $(sort $(qesf-parts-y))
$(warning "parts: $(qesf-parts-y)")

PHONY += $(qesf-parts-y)
$(qesf-parts-y):
	$(MAKE) -C $@

PHONY += qesf_parts_build
qesf_parts_build: $(qesf-parts-y)

PHONY += qesf_library_build
qesf_library_build:
	$(AR) $(ARCH_LDFLAGS) cq $(BUILD_DIR)/$(QESFLIB) $(BUILD_OJB_DIR)/*.o


PHONY += qesf_library_install
qesf_library_install: create_install_dir
	cp $(KCONFIG_OUT_HEADER) $(INSTALL_INC_DIR)
	for dir in $(qesf-parts-y); do \
		$(MAKE) -C $$dir install; \
	done
	cp $(BUILD_DIR)/$(QESFLIB) $(INSTALL_LIB_DIR)

create_install_dir: $(INSTALL_DIR) $(INSTALL_INC_DIR) $(INSTALL_LIB_DIR)

$(INSTALL_DIR):
	mkdir -p $(INSTALL_DIR)

$(INSTALL_INC_DIR):
	mkdir -p $(INSTALL_INC_DIR)

$(INSTALL_LIB_DIR):
	mkdir -p $(INSTALL_LIB_DIR)

PHONY += clean
clean:
	rm -r $(BUILD_DIR)

.PHONY: $(PHONY)